var OrgTreeObj;
var OrgTreeNode = null; //选中机构树节点
var userlist;
var selectMaps = {};
var currentOrgId = "", currentRoleId = "", currentPostId = "", currentDutyId = "";
var orgid = RequestURLParam.getParam("orgid");
var type = RequestURLParam.getParam("type");
var postid = RequestURLParam.getParam("postid");
var dutyid = RequestURLParam.getParam("dutyid");
var roleid = RequestURLParam.getParam("roleid");
var MultiSelect = RequestURLParam.getParam("MultiSelect");
$(document).ready(function () {
    userlist = mini.get('usergrid');
    if (MultiSelect != null) {
        userlist.setMultiSelect(true);
    }
    if (type == "org") {
        $("#maingrid").remove();
        OrgTreeObj = mini.get("OrgTree");
        initOrgTree();
    } else if (type == "post") {
        $("#OrgTree").remove();
        var posthtml = '<div id="Postgrid" class="mini-datagrid" style="width:100%;height:100%;" url="OrgDutyPostView!pageList.do" onrowclick="Postselect" idField="postId" allowResize="true" sizeList="[20,50,100]" pageSize="20" pageIndexField="page" pageSizeField="rows" dataField="rows">' +
            '<div property="columns"><div field="postName" width="150" headerAlign="center" allowSort="true">岗位名称</div></div></div>';
        $("#maingrid").html(posthtml);
        mini.parse();
        var postlist = mini.get("Postgrid");
        postlist.load({"orgDutyPostView.postId": postid}, function () {
            postlist.select(0);
            var row = postlist.getSelected();
            currentPostId = row.postId;
            refreshUserGrid();
        });
    } else if (type == "duty") {
        $("#OrgTree").remove();
        var dutyhtml = '<div id="Dutygrid" class="mini-datagrid" style="width:100%;height:100%;" url="sysDuty!pageList.do" onrowclick="Dutyselect" idField="dutyId" allowResize="true" sizeList="[20,50,100]" pageSize="20" pageIndexField="page" pageSizeField="rows" dataField="rows">' +
            '<div property="columns"><div field="dutyName" width="150" headerAlign="center" allowSort="true">职务名称</div></div></div>';
        $("#maingrid").html(dutyhtml);
        mini.parse();
        var dutylist = mini.get("Dutygrid");
        dutylist.load({"sysDuty.dutyId": dutyid}, function () {
            dutylist.select(0);
            var row = dutylist.getSelected();
            currentDutyId = row.dutyId;
            refreshUserGrid();
        });
    } else if (type == "role") {
        $("#OrgTree").remove();
        var rolehtml = '<div id="Rolegrid" class="mini-datagrid" style="width:100%;height:100%;" url="sysRole!pageList.do" onrowclick="Roleselect" idField="roleId" allowResize="true" sizeList="[20,50,100]" pageSize="20" pageIndexField="page" pageSizeField="rows" dataField="rows">' +
            '<div property="columns"><div field="roleName" width="150" headerAlign="center" allowSort="true">角色名称</div></div></div>';
        $("#maingrid").html(rolehtml);
        mini.parse();
        var rolelist = mini.get("Rolegrid");
        rolelist.load({"sysRole.roleId": roleid}, function () {
            rolelist.select(0);
            var row = rolelist.getSelected();
            currentRoleId = row.roleId;
            refreshUserGrid();
        });
    }
});

/***
 * 加载机构树
 * */
function initOrgTree() {
    OrgTreeNode = null;
    Tree.getTree(OrgTreeObj, 'sysOrg!list.do?sysOrg.orgPath=' + orgid, "orgId", "orgPid");
}

/***
 * 机构树选择事件
 * */
function OrgTreeselected(e) {
    var OrgTree = e.sender;
    var node = e.node;
    OrgTreeNode = node;
    currentOrgNode = OrgTreeNode;
    currentOrgId = OrgTreeNode.orgId;
    currentOrgName = OrgTreeNode.orgName;
    refreshUserGrid();
}

/***
 * 岗位选择选择事件
 * */
function Postselect(e) {
    currentPostId = e.record.postId;
    refreshUserGrid();
}

/***
 * 职务选择选择事件
 * */
function Dutyselect(e) {
    currentDutyId = e.record.dutyId;
    refreshUserGrid();
}

/***
 * 角色选择选择事件
 * */
function Roleselect(e) {
    currentRoleId = e.record.roleId;
    refreshUserGrid();
}

/***
 * 刷新人员列表
 * */
function refreshUserGrid() {
    var prms = {};
    var Usertitle = "";
    if (currentOrgId && currentOrgId.trim() != "") {
        prms["orgUserView.orgPath"] = currentOrgId;
        userlist.load(prms);
    }
    if (currentPostId && currentPostId.trim() != "") {
        prms["orgUserView.upostId"] = currentPostId;
        userlist.load(prms);
    }
    if (currentDutyId && currentDutyId.trim() != "") {
        prms["orgUserView.dutyId"] = currentDutyId;
        userlist.load(prms);
    }
    if (currentRoleId && currentRoleId.trim() != "") {
        prms["orgUserView.roleId"] = currentRoleId;
        userlist.load(prms);
    }
    /* if (UserDeptId && UserDeptId.trim() != "") {
         prms["orgUserView.orgPath"] = UserDeptId;
         Usertitle += '<a class="mini-button mini-button-iconRight" plain="true" style="color:red;" iconCls="icon-no" onclick="UserClear(\'Dept\')">--' + UserDeptName + '</a>';
     }
     if (UserPostId && UserPostId.trim() != "") {
         prms["orgUserView.uuserId"] = UserPostId;
         Usertitle += '<a class="mini-button mini-button-iconRight" plain="true" style="color:red;" iconCls="icon-no" onclick="UserClear(\'Post\')">--' + UserPostName + '</a>';
     }
     if (PostDutyId && PostDutyId.trim() != "") {
         prms["orgUserView.dutyId"] = PostDutyId;
     }
     if (UserName && UserName.trim() != "") {
         prms["orgUserView.key"] = UserName;
     } else {
         $("#Userkey").val("");
     }*/
}


function selUser() {
    var id = [];
    var name = [];
    var deptName = [];
    var deptId = [];
    var deptPath = [];
    var count = 0;
    for (var key in selectMaps) {
        id.push(selectMaps[key].userId);
        name.push(selectMaps[key].userName);
        deptName.push(selectMaps[key].orgName);
        deptId.push(selectMaps[key].orgId);
        deptPath.push(selectMaps[key].orgPath);
        count++;
    }
    if (count > 0) {
        window.CloseOwnerWindow({
            'id': id.join(","),
            'name': name.join(","),
            'deptName': deptName.join(","),
            'deptId': deptId.join(","),
            'deptPath': deptPath.join(",")
        });
    } else {
        mini.alert("请勾选!");
    }
}

/*
 * 在表格加载的时候激发事件
 * */
function onGridLoad(e) {
    for (var i = 0; i < userlist.data.length; i++) {
        for (var key in selectMaps) {
            if (key == userlist.data[i].userId) {
                userlist.setSelected(userlist.getRow(i));
            }
        }
    }
}

/*
 * 在表格选择行时
 * */
function usergridclick(e) {
    if (e.records && e.records.length > 0) {
        for (var i = 0; i < e.records.length; i++) {
            delete selectMaps[e.records[i].userId];
        }
    } else {
        delete selectMaps[e.record.userId];
    }
    var rows = userlist.getSelecteds();
    for (var i = 0; i < rows.length; i++) {
        selectMaps[rows[i].userId] = rows[i];
    }
    int_selectedUser();
}

/*
 * 显示选中的人员
 */
function int_selectedUser() {
    var Usertitle = "";
    for (var key in selectMaps) {
        Usertitle += '<a class="mini-button mini-button-iconRight" plain="true" style="color:red;" iconCls="icon-no" onclick="UserClear(\'' + key + '\')">' + selectMaps[key].userName + '</a>';
    }
    $("#UserSelected").html(Usertitle);
    mini.parse();
}

/*
 * 删除选中的岗位
 */
function UserClear(key) {
    delete selectMaps[key];
    for (var i = 0; i < userlist.data.length; i++) {
        if (key == userlist.data[i].userId) {
            userlist.deselect(userlist.getRow(i));
        }
    }
    int_selectedUser();
}